<?php

// +----------------------------------------------------------------------

// | OneThink [ WE CAN DO IT JUST THINK IT ]

// +----------------------------------------------------------------------

// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.

// +----------------------------------------------------------------------

// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>

// +----------------------------------------------------------------------

namespace Home\Controller;

use Think\Controller;

/**

 * 前台首页控制器

 * 主要获取首页聚合数据

 */

class PublicController extends Controller
{

    //登录

    public function index()
    {

        header("Content-Type:text/html;charset=utf-8");
        D('member')->startTrans();

        D('ucenter_member')->startTrans();

        D('core_user_info')->startTrans();

        $con['dksign'] = I('dksign');

        $user = M('core_data')->where($con)->find();

        if (!$user) {

            echo "<script>";

            echo "alert('用户不存在');";

            echo "window.location.href='./index.php?s=/Home/Public/login'; ";

            echo "</script>";

        } else {

            $data['username'] = I('dksign');

            $data['password'] = md5(substr(I('dksign'), -6));

            $result = D('ucenter_member')->add($data);

            //根据unit查出 省市区

            //注册信息关联表
            $map1 = D('core_unit')->where("id=" . $user['unit'])->field('province,city,district')->
                find();
            $result2 = D('member')->add(array('nickname' => $user['realname'], 'uid' => $result, 'status' => 1, 'pos_city' => $map1['city'], 'pos_district' => $map1['district'], 'pos_province' => $map1['province']));

            $con['title'] = $user['depart'];

            $con['unit'] = $user['unit'];

            $class = M('core_class')->where($con)->find();

            if (!$class&&($user['group']==5||$user['group']==4)) {

                //如果所属班不存在，自动生成班级
                $con['status'] = 1;
                $result3 = M('core_class')->add($con);

            } else {

                $result3 = $class['id'];

            }

            //把所有信息保存到信息关联表里user_info

            $aaa['class_id'] = $result3;

            $aaa['unit_id'] = $user['unit'];

            $aaa['uid'] = $result;

            $aaa['group_id'] = $user['group'];

            $r = D('core_user_info')->add($aaa);
           
            if ($result && $result2 && $r) {
                D('member')->commit();

                D('ucenter_member')->commit();

                D('core_user_info')->commit();
                $u = M('core_unit')->where('id = '.$user['unit'])->getField('title');
                $_SESSION['oc_home']['uid']      = $result;
                $_SESSION['oc_home']['username'] = I('dksign');
                $_SESSION['oc_home']['class']    = $result3;
                $_SESSION['oc_home']['unit']     = $user['unit'];
                $_SESSION['oc_home']['unit_title']= $u;
                $_SESSION['oc_home']['group']    = $user['group'];
                echo "<script>";

                echo "alert('登录成功');";

                echo "window.location.href='./index.php?s=/Home/Public/person'; ";

                echo "</script>";

            } else {

                D('member')->rollback();

                D('ucenter_member')->rollback();

                D('core_user_info')->rollback();

                echo "<script>";

                echo "alert('登录失败，密码错误');";

                echo "window.location.href='./index.php?s=/Home/Public/login'; ";

                echo "</script>";

            }

        }

    }

    //登录
    public function login()
    {
        //header("Content-Type:text/html;charset=utf-8");
        if (IS_POST) {
            $con['username|mobile'] = I('username');
            $con['password']        = md5(I('password'));
            $result                 = M('core_userinfo_view')->where($con)->find();
            if ($result) {
                $_SESSION['oc_home']['uid']      = $result['uid'];
                $_SESSION['oc_home']['username'] = $result['username'];
                $_SESSION['oc_home']['nickname'] = $result['nickname'];
                $_SESSION['oc_home']['group']    = $result['group_id'];
                $_SESSION['oc_home']['class']    = $result['class_id'];
                $_SESSION['oc_home']['unit']     = $result['unit_id'];
                //dump($_SESSION['oc_home']);exit();
                if (isMobile()) {
                    $this->redirect('person');
                } else {
                    $this->redirect('Index/index');
                }
                /*判断是否是手机版*/
            } else {
                if (isMobile()) {
                    $this->redirect('index', array('dksign' => I('username')));
                } else {
                    $this->redirect('Index/index');
                }
            }
        } else {
            $this->assign('realname', I('realname'));
            $this->assign('dksign', I('dksign'));
            $this->display();
        }

    }

    //登出
    public function logout()
    {
        $_SESSION['oc_home'] = '';
        if (isMobile()) {
            $this->redirect('login');
        } else {
            $this->redirect('Index/index');
        }
    }

    //个人中心

    public function person()
    {

        $data = M('core_userinfo_view')->where('uid = ' . $_SESSION['oc_home']['uid'])->find();



        $this->assign('data', $data);

        $affiche = M('affiche')->find();

        $this->assign('affiche', $affiche);

        $this->display();

    }

    public function center()
    {
        $map['uid']=$_SESSION['oc_home']['uid'];
        $map['status']=array('in','1,0');
        $data  = M('core_userinfo_view')->where('uid = ' . $_SESSION['oc_home']['uid'])->find();
        $list  = M('core_leave')->where($map)->select();
        $f     = $_SESSION['oc_home']['uid'];
        $dave  = M('core_pay_order')->where('uid=' . $f)->select();
        $da    = count($dave);
        $count = count($list);
        $this->assign('data', $data);
        $this->assign('count', $count);
        $this->assign('da', $da);
        $this->display();
    }
    //更改电话身份证
    public function setInfo()
    {
        header("Content-Type:text/html;charset=utf-8");
        $map['username'] = $_SESSION['oc_home']['username'];
        if(I('name')){
        
            $qw=M('core_userinfo_view')->where($map)->field('mobile')->find();
            
            $data['mobile'] = I('name');
            if($qw){
                $shouji=M('core_userinfo_view')->where($map)->save($data);
                echo "<script>";
                echo"alert('更改状态成功');";
                echo "window.location.href='./index.php?s=/Home/public/center.html'; ";
                echo"</script>";
            }else{
                $shouji=M('core_userinfo_view')->where($map)->add($data);
                 echo "<script>";
                echo"alert('更改状态成功');";
                echo "window.location.href='./index.php?s=/Home/public/center.html'; ";
                echo"</script>";
            }
        
        }
        if(I('sfz')){
           
        
            $qww=M('core_userinfo_view')->where($map)->field('qq')->find();
            
            $data['qq'] = I('sfz');
            if($qww){
                $shouji=M('core_userinfo_view')->where($map)->save($data);
                 echo "<script>";
                 echo"alert('更改状态成功');";
                 echo "window.location.href='./index.php?s=/Home/public/center.html'; ";
                 echo"</script>";
            }else{
                $shouji=M('core_userinfo_view')->where($map)->add($data);
                 echo "<script>";
                 echo"alert('更改状态成功');";
                 echo "window.location.href='./index.php?s=/Home/public/center.html'; ";
                 echo"</script>";
            }
        }
        
        $list=M('core_userinfo_view')->where($map)->find();
        $this->assign('list',$list);
        $this->display();
}

    //-----------获取下级----------------@
    public function getChild()
    {

        $prov = $_GET['child'];

        if ($_GET['child']) {

            $childModel = M('district');

            $child = $childModel->where('upid = ' . $prov)->select();

            echo json_encode($child);

        }

    }

    public function phone()
    {

        header("Content-type: text/html; charset=utf-8");
        if (IS_POST) {
            // $data=$_SESSION['oc_home']['username'];
            $data = I('name');
            if (I('passwd1') == I('passwd2')) {
                $f['password'] = md5(I('passwd1'));
                $list          = M('ucenter_member')->where(array('username' => $data))->save($f);
                echo "<script>";
                echo "alert('更新成功');";
                echo "window.location.href='./index.php?s=/Home/Public/person'; ";
                echo "</script>";
            } else {
                echo "<script>";
                echo "alert('两次密码输入不一致');";
                echo "window.location.href='./index.php?s=/Home/Public/phone'; ";
                echo "</script>";
            }

        } else {
            $this->display();
        }

    }

    public function affiche()
    {

        $affiche = M('affiche')->find();

        $this->assign('affiche', $affiche);

        $this->display();

    }
    public function password()
    {
        header("Content-type: text/html; charset=utf-8");
        if (IS_POST) {
            $data = $_SESSION['oc_home']['username'];
            $f    = M('ucenter_member')->where(array('username' => $data))->find();
            $s    = $f['password'];
            $b    = md5(I('name'));
            if ($s == $b) {
                if (I('passwd1') == I('passwd2')) {
                    $f['password'] = md5(I('passwd1'));
                    $list          = M('ucenter_member')->where(array('username' => $data))->save($f);
                    echo "<script>";
                    echo "alert('更新成功');";
                    echo "window.location.href='./index.php?s=/Home/Public/person'; ";
                    echo "</script>";
                } else {
                    echo "<script>";
                    echo "alert('两次输入密码不一致');";
                    echo "window.location.href='./index.php?s=/Home/Public/password'; ";
                    echo "</script>";
                }
            } else {
                echo "<script>";
                echo "alert('请输入原密码');";
                echo "window.location.href='./index.php?s=/Home/Public/password'; ";
                echo "</script>";
            }
        } else {
            $this->display();
        }
    }

    public function TeacherpayXQ()
    {
        $map['status'] = 1;
        if ($_SESSION['oc_home']['group'] == '4') {
            $map['class_id'] = $_SESSION['oc_home']['class'];
        }
        $map['unit_id']  = $_SESSION['oc_home']['unit'];
        $map['group_id'] = 5;

        $map['create_time']           = M('core_pay_bill_view')->where($map)->max('create_time');

       
        $list            = M('core_pay_bill_view')->where($map)->group("nickname")->select();
        $ff              = M('core_userinfo_view')->where('class_id=' . $_SESSION['oc_home']['class'])->getField('unit_title');
        $data            = M('core_userinfo_view')->where('class_id=' . $_SESSION['oc_home']['class'])->find();
         // $zong = M('core_userinfo_view')->where($map)->count();
         
        $succ            = M('core_pay_bill_view')->where('pay_state = 1')->where($map)->count('distinct nickname');
        $eroo            = M('core_pay_bill_view')->where('pay_state = 0')->where($map)->count('distinct nickname');

        $this->assign('list', $list);
        
        $this->assign('ff', $ff);
        $this->assign('data', $data);
        $this->assign('succ', $succ);
        $this->assign('eroo', $eroo);
        $this->display();
    }

    public function editaffiche()
    {
        header("Content-type: text/html; charset=utf-8");
        if (IS_POST) {

            $data['affiche'] = I('affiche');

            $la = M('affiche')->where('id=1')->save($data);

            echo "<script>";
            echo "alert('编辑成功');";
            echo "window.location.href='./index.php?s=/Home/Public/person'; ";
            echo "</script>";

        } else {
            $this->assign('aff', I('aff'));
            $this->display();
        }

    }

    public function teacherbaby()
    {
        if (I('nickname')) {
            //查询关键字进行搜索。
            $nickname        = I('nickname');
            $map['nickname'] = array('like', '%' . $nickname . '%');
            $this->assign('nickname', $nickname);
        }
        if ($_SESSION['oc_home']['group'] == '4') {
            $map['class_id'] = $_SESSION['oc_home']['class'];
            $map['unit_id']=$_SESSION['oc_home']['unit'];
            $map['group_id']=5;
            $list = M('core_userinfo_view')->where($map)->select();
        }
        switch ($_SESSION['oc_home']['group']) {
            case '6':
                $fy = M('core_unit')->where('pid='.$_SESSION['oc_home']['unit'])->getField('id',true);
                //查总园下面所有分园
                $map['unit_id'] = array('in',implode(',',$fy));
                $map['group_id']=5;
                $list = M('core_userinfo_view')->where($map)->select();
                //查班级用
                $_SESSION['oc_home']['fy'] = implode(',',$fy);
                break;
            case '3':
                $map['unit_id'] = $_SESSION['oc_home']['unit'];
                $map['group_id']=5;
                $list = M('core_userinfo_view')->where($map)->select();
                //查班级用
                $_SESSION['oc_home']['fy'] = $_SESSION['oc_home']['unit'];
                break;                
        }
        $this->assign('list', $list);
        $this->display();
    }
    //获得达客id
    public function getDKsign($unit)
    {
        $d['dksign']      = create_DKsign('ns', $unit);
        $d['create_time'] = time();
        $result           = M('core_dksign')->add($d);
        if (!$result) {
            getDKsign($unit);
        }
        return $d['dksign'];
    }

}
